#!/bin/sh

# 环境变量设置
source ~/.vis_profile

# start services.
start()
{
    # 目录准备
    mkdir -p ${VIS_LOG_PATH}
    mkdir -p ${VIS_UPLOAD_PATH}
    rm -rf ${VIS_FILE_ROOT_PATH}/live/*;
    rm -rf ${VIS_FILE_ROOT_PATH}/flv/*.*.*

    # 文件准备
    mkdir -p ${VIS_FILE_ROOT_PATH}/thumbnail/bak
    cp -f ${VIS_ROOT_PATH}/manager/etc/resource/default.png ${VIS_FILE_ROOT_PATH}/thumbnail
    
    # 启动
    # nohup python ${VIS_ROOT_PATH}/manager/src/manager.py ${RESTFUL_HTTP_PORT} &
    python ${VIS_ROOT_PATH}/manager/src/manager.py ${RESTFUL_HTTP_PORT} &
}

stop()
{
    # 停止
    PIDS=`ps -ef | egrep "manager.py" | grep -v "grep" | awk '{print $2}'`
    
    while true; do
        if [ -z "$PIDS" ]
        then
            break
        fi
        for PID in $PIDS; do
            kill -9 $PID 2>/dev/null
        done
        PIDS=`ps -ef | egrep "manager.py" | grep -v "grep" | awk '{print $2}'`
    done
}

RETVAL=0
# execute parameter:
#  @$1  start|stop|restart|...
#  @$2  shell file name.
execute()
{
    case "$1" in
        start)
            start
        ;;
        
        stop)
            stop
        ;;
        
        restart)
            stop
            start
        ;;
        
        *)
            echo $"Usage: $2 {start|stop|restart|reload|status}"
            RETVAL=1
            exit $RETVAL
    esac
}

# execute shell file.
execute "$1" "$0"

RETVAL=$?

exit $RETVAL
